package equipmentManager.dao;

import equipmentManager.bean.Equipment;
import equipmentManager.utils.Utils;

public class EquipmentDao {
    // 最多存几种设备
    private final int MAX = 10;
    // 设备数据源，数据库
    private Equipment[] equipmentArr = new Equipment[MAX];
    // 指向数据库中的最后一个空白位置
    private int pos=0;
    //借还记录
    private String lendInfo = "";

    public EquipmentDao() {
        //初始化数组
//        equipmentArr[0] = new Equipment("手机","华为",0,5,5,3);
//        pos++;
//        equipmentArr[1] = new Equipment("投影仪","牧马人",0,0,6,0);
//        pos++;
//        equipmentArr[2] = new Equipment("电脑","thinkpad",0,0,0,1);
//        pos++;
    }

    /**
     * 归还
     */
    public void returnBack(){
        //有设备
        System.out.print("输入归还人名字：");
        String name = Utils.getString("\\S{1,9}");
        System.out.print("输入设备名字：");
        String equipmentName = Utils.getString("\\S{1,9}");
        System.out.print("输入设备类型：");
        String equipmentType = Utils.getString("\\S{1,9}");

        //寻找该设备
        Equipment equipment = getEquipmentByNameAndType(equipmentName,equipmentType);
        if(equipment==null || equipment.getUseableNum()==0){
            //设备不存在
            System.out.println("暂无该设备，无需归还！");
            return;
        }
        System.out.print("输入设备数量：");
        int equipmentNum = Integer.parseInt(Utils.getString("[1-9]\\d*"));
        if(equipment.getLendNum()<equipmentNum){
            //设备不存在
            System.out.println("归还数量超出借出，无法归还！");
            return;
        }
        //条件具备，可以归还
        //更新可用库存
        equipment.setUseableNum(equipment.getUseableNum()+equipmentNum);
        equipment.setLendNum(equipment.getLendNum()-equipmentNum);
        // 添加借出日志
        lendInfo += name+"归还了"+equipmentNum+"个,名字为："+equipmentName+",型号为："+equipmentType+"\n";
        System.out.println("归还成功！");
    }

    /**
     * 借出设备
     */
    public void lend(){
        if(pos==0){
            System.out.println("暂无设备，无法外借！");
            return;
        }
        //有设备
        System.out.print("输入借设备人名字：");
        String name = Utils.getString("\\S{1,9}");
        System.out.print("输入设备名字：");
        String equipmentName = Utils.getString("\\S{1,9}");
        System.out.print("输入设备类型：");
        String equipmentType = Utils.getString("\\S{1,9}");

        //寻找该设备
        Equipment equipment = getEquipmentByNameAndType(equipmentName,equipmentType);
        if(equipment==null || equipment.getUseableNum()==0){
            //设备不存在
            System.out.println("暂无该设备，无法外借！");
            return;
        }
        System.out.print("输入设备数量：");
        int equipmentNum = Integer.parseInt(Utils.getString("[1-9]\\d*"));
        if(equipment.getUseableNum()<equipmentNum){
            //设备不存在
            System.out.println("设备不够，无法外借！");
            return;
        }
        //条件具备，可以外界
        //更新可用库存
        equipment.setUseableNum(equipment.getUseableNum()-equipmentNum);
        equipment.setLendNum(equipment.getLendNum()+equipmentNum);
        // 添加借出日志
        lendInfo += name+"借出了"+equipmentNum+"个,名字为："+equipmentName+",型号为："+equipmentType+"\n";
        System.out.println("借出成功！");
    }

    /**
     * 删除
     */
    public void delete(){
        System.out.println("==删除==");
        System.out.println("\t 1. 删除所有");
        System.out.println("\t 2. 根据名称删除");
        System.out.print("\t 请选择菜单：");
        String menu = Utils.getString("[12]");
        switch (menu) {
            case "1":
                clear();
                break;
            case "2":
                System.out.print("请输入设备名：");
                deleteByName(Utils.getString("\\S{1,9}"));
                break;
        }
    }

    /**
     * 根据名称清删除
     */
    private void deleteByName(String name){
        //假如此设备不存在
        boolean isHave = false;
        for (int i = 0; i < pos; i++) {
            Equipment equipment = equipmentArr[i];
            if(equipment.getName().equals(name)){
                //名字匹配成功
                isHave = true;
                equipmentArr[i]=null;
            }
        }
        if(isHave){
            //有，有空穴，整理数据
            Equipment[] arr = new Equipment[MAX];
            //临时指向新数组最左侧的空位置
            int index = 0;
            for (int i = 0; i < equipmentArr.length; i++) {
                if(equipmentArr[i]!=null){
                    arr[index] = equipmentArr[i];
                    index++;
                }
            }
            equipmentArr = arr;
            //更新pos
            pos = index;
            System.out.println("找到设备，删除成功！");
        }else {
            //没有
            System.out.println("没有此设备，删除失败！");
        }
    }

    /**
     * 清空所有设备
     */
    private void clear(){
        System.out.print("确定要清空数据吗？(y/n)：");
        if(Utils.getYN().equals("Y")){
            //清楚内存
            for (int i = 0; i < pos; i++) {
                equipmentArr[i] = null;
            }
            pos=0;
            System.out.println("清空成功！");
            return;
        }
        System.out.println("取消清空！");
    }
    //添加设备
    public void add() {
        if(pos==MAX){
            System.out.println("仓库种类已满，删除后重试！");
            return;
        }
        //创建一个设备
        Equipment equipment = new Equipment();
        //允许添加，输入设备信息
        System.out.print("输入设备名字：");
        equipment.setName(Utils.getString("\\S{1,10}"));
        System.out.print("输入设备类型：");
        equipment.setType(Utils.getString("\\S{1,10}"));
        System.out.print("输入设备数量：");
        int num = Integer.parseInt(Utils.getString("[1-9]\\d*"));
        System.out.print("输入设备状态(1可用，2借出，3损坏)：");
        String state = Utils.getString("[1-3]");
        equipment.setNumByState(num,state);
        //放入数组
        //判断设备是否存在
        Equipment temp = getEquipmentByNameAndType(equipment.getName(), equipment.getType());
        if(temp==null){
            //此种设备不存在，直接新增
            equipmentArr[pos] = equipment;
            pos++;
            return;
        }
        //存在，更新对应状态的num即可
        switch (state){
            case "1":
                temp.setUseableNum(temp.getUseableNum()+num);
                break;
            case "2":
                temp.setLendNum(temp.getLendNum()+num);
                break;
            case "3":
                temp.setBreakNum(temp.getBreakNum()+num);
                break;
        }
        System.out.println("==设备添加成功！==");
    }

    /**
     * 判断设备是否存在
     * @param name
     * @param type
     * @return
     */
    public Equipment getEquipmentByNameAndType(String name,String type){
        for (int i = 0; i < pos; i++) {
            Equipment temp = equipmentArr[i];
            if(temp.getName().equals(name) && temp.getType().equals(type)){
                //找到了
                return temp;
            }
        }
        //没找到
        return null;
    }

    /**
     * 查询设备
     */
    public void query() {
        System.out.println("==查询==");
        System.out.println("\t 1. 查询所有");
        System.out.println("\t 2. 根据名称查询");
        System.out.println("\t 3. 根据名称、型号查询");
        System.out.println("\t 4. 查询设备借还日志");
        System.out.print("\t 请选择菜单：");
        String menu = Utils.getString("[1-4]");
        switch (menu){
            case "1":
                queryAll();
                break;
            case "2":
                System.out.print("输入设备名字：");
                queryByName(Utils.getString("\\S{1,10}"));
                break;
            case "4":
                if(lendInfo.length()==0){
                    System.out.println("暂无借用记录！");
                }else {
                    System.out.print(lendInfo);
                }
                break;


        }

    }

    /**
     * 根据名字查询
     * @param name
     */
    private void queryByName(String name){
        if (pos==0){
            System.out.println("目前没有设备，添加后重试！");
            return;
        }

        System.out.println("设备名称\t型号\t库存\t状态\t入库时间");
        //假如一个都没有
        boolean isHave = false;
        for (int i = 0; i < pos; i++) {
            Equipment equipment = equipmentArr[i];
            if(!equipment.getName().equals(name)){
                //名字不符合
                continue;
            }
            isHave = true;
            printEquipmentAsTable(equipmentArr[i]);
        }
        if(!isHave){
            System.out.println("查无此设备！");
        }
    }
    /**
     * 查询所有设备
     */
    private void queryAll(){
        if (pos==0){
            System.out.println("目前没有设备，添加后重试！");
            return;
        }
        System.out.println("设备名称\t型号\t库存\t状态\t入库时间");
        //
        for (int i = 0; i < pos; i++) {
            printEquipmentAsTable(equipmentArr[i]);
        }
    }

    /**
     * 以表格形式打印设备
     * @param equipment
     */
    private void printEquipmentAsTable(Equipment equipment){
        String info = equipment.getName()+"\t"+equipment.getType()+"\t";
        if(equipment.getUseableNum()>0){
            System.out.print(info+equipment.getUseableNum()+"\t");
            System.out.print("可用\t");
            System.out.println(equipment.getTime());
        }
        if(equipment.getLendNum()>0){
            System.out.print(info+equipment.getLendNum()+"\t");
            System.out.print("借出\t");
            System.out.println(equipment.getTime());
        }
        if(equipment.getBreakNum()>0){
            System.out.print(info+equipment.getBreakNum()+"\t");
            System.out.print("损坏\t");
            System.out.println(equipment.getTime());
        }

    }

}
